package cn.com.xy.sms.sdk.number;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.LruCache;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.entity.NumberInfo;
import cn.com.xy.sms.sdk.db.entity.NumberInfoManager;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.util.StringUtils;

/* SDK-428 huangzhiqiang 20160615 start */
public class ParseNumberCache {

    // 号码对应识别信息
    private static final LruCache<String, JSONObject> EFFECTIVE_DATA_CACHE = new LruCache<String, JSONObject>(500);
    private static final Set<String> INVALID_NUM = Collections.synchronizedSet(new HashSet<String>()) ;

    public static JSONObject getCache(String num) {
        return EFFECTIVE_DATA_CACHE.get(num);
    }

    public static void addCache(String num, JSONObject data) {
        EFFECTIVE_DATA_CACHE.put(num, data);
    }

    /**
     * 删除有效缓中指定号码信息
     * 
     * @param num 号码
     */
    public static void removeCache(String num) {
        EFFECTIVE_DATA_CACHE.remove(num);
    }

    public static void addInvalidNum(String num) {
        INVALID_NUM.add(num);
    }

    public static void removeInvalidNum(String num) {
        INVALID_NUM.remove(num);
    }

    public static boolean isInvalidNum(String num) {
        return INVALID_NUM.contains(num);
    }

    /**
     * 删除所有有效缓存及无效号码集合
     */
    public static void removeAllCache() {
        EFFECTIVE_DATA_CACHE.evictAll();
        INVALID_NUM.clear();
    }

    /**
     * 更新号码缓存数据
     * 
     * @param numInfoArr
     */
    public static void updateNumCache(JSONArray numInfoArr) {
        try {
            if (numInfoArr == null || numInfoArr.length() == 0) {
                return;
            }

            int length = numInfoArr.length();
            for (int i = 0; i < length; i++) {
                JSONObject numInfo = numInfoArr.getJSONObject(i);
                String num = numInfo.getString(NumberInfo.NUM_KEY);
                addCache(num, numInfo);
            }
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "ParseNumberCache updateNumCache error:", e);
        }
    }

    /**
     * 批量更新缓存里的用户标记上传状态
     * 
     * @param nums
     */
    public static void updateUserTagUploadStatusCache(JSONArray nums) {
        try {
            if (nums == null || nums.length() == 0) {
                return;
            }

            int length = nums.length();
            for (int i = 0; i < length; i++) {
                String num = nums.getString(i);
                updateUserTagUploadStatusCache(num, NumberInfo.UPLOAD_STATUS_UPLOADED);
            }
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "ParseNumberCache updateUserTagUploadStatusCache error:", e);
        }
    }

    /**
     * 更新缓存里的用户标记信息
     * 
     * @param num
     * @param tag 用户标记内容
     * @param tagType 用户标记类型值key(1:系统预制类型 2:自定义类型)
     * @param uploadStatus 用户标记上传状态值key(0:未上传 1:已上传)
     */
    public static void updateUserTagCache(String num, String tag, int tagType, int uploadStatus) {
        try {
            JSONObject data = ParseNumberCache.getCache(num);
            if (data != null) {
                data.put(NumberInfo.USER_TAG_KEY, tag);
                data.put(NumberInfo.USER_TAG_TYPE_KEY, tagType);
                data.put(NumberInfo.USER_TAG_UPLOAD_STATUS_KEY, uploadStatus);
            }
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "ParseNumberCache updateUserTagCache error:", e);
        }
    }

    /**
     * 更新缓存里的用户标记上传状态
     * 
     * @param num
     * @param uploadStatus 用户标记上传状态值key(0:未上传 1:已上传)
     */
    public static void updateUserTagUploadStatusCache(String num, int uploadStatus) {
        try {
            JSONObject data = ParseNumberCache.getCache(num);
            if (data != null) {
                data.put(NumberInfo.USER_TAG_UPLOAD_STATUS_KEY, uploadStatus);
            }
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "ParseNumberCache updateUserTagUploadStatusCache error:", e);
        }
    }

    /**
     * 删除缓存里的用户标记数据
     * 
     * @param num
     */
    public static void removeUserTag(String num) {
        try {
            JSONObject numInfo = ParseNumberCache.getCache(num);
            if (numInfo != null) {
                NumberInfoManager.removeUserTag(numInfo);
                // 删除用户标记后只剩下号码一个Key则代表这个号码仅有用户标记没有其他数据，将号码从有效数据缓存中删除
                if (numInfo.length() == 1 && numInfo.has(NumberInfo.NUM_KEY)) {
                    ParseNumberCache.removeCache(num);
                }
            }
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "ParseNumberCache removeUserTag error:", e);
        }
    }

    /**
     * 从缓存或数据库中查询号码信息
     * 
     * @param num
     * @return
     */
    public static JSONObject queryByNum(String num) {
        if (StringUtils.isNull(num)) {
            return null;
        }
        JSONObject numInfo = getCache(num);
        if (numInfo != null) {
            return numInfo;
        }
        numInfo = NumberInfoManager.queryNumberInfoByDB(num);
        if (numInfo != null && NumberInfoManager.isEffectiveData(numInfo)) {
            addCache(num, numInfo);
        }
        return numInfo;
    }
}
/* SDK-428 huangzhiqiang 20160615 end */
